package com.ymatou.iapi.liveseller.testcase;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.ymatou.iapi.liveseller.parameter.SellerLiveListBean;
import com.ymatou.iapi.liveseller.service.SellerLiveListCall;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.Userinfo;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LiveRoomWapper;

/**
 * 买手直播列表 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SellerLiveList {
	private static SellerLiveListBean sellerlivelistBean;
	private static SellerLiveListCall sellerlivelistCall;
	private static LiveRoomWapper liveRoomWapper;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("买手直播列表");
	}

	@Before
	public void caseUp() {
		sellerlivelistBean = new SellerLiveListBean();
		sellerlivelistCall = new SellerLiveListCall();
		liveRoomWapper = new LiveRoomWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(sellerlivelistCall.getOpurl());
		liveRoomWapper = null;
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveList_001() {
		Logger.start(true, "查询买手直播列表成功");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			sellerlivelistBean.setUserId(createlive.getUserId());
			sellerlivelistBean.setPageIndex(1);
			sellerlivelistBean.setPageSize(10);
			sellerlivelistCall.setData(sellerlivelistBean);
			sellerlivelistCall.callService();
			Logger.verifyEquals("200", sellerlivelistCall.getString("Code"), "验证Code");
			long count = liveRoomWapper.selectCountLiveroom(createlive.getUserId());
			Logger.verifyEquals(count, sellerlivelistCall.getJsonObject().get("Data").getAsJsonObject().get("Totals").getAsLong(), "验证count");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SellerLiveList_002() {
		Logger.start(true, "翻页查询买手直播列表成功");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			int index = 1;
			int size=2;
			boolean hasnext = true;
			Set liveset=new HashSet<>();
			sellerlivelistBean.setUserId(createlive.getUserId());
			sellerlivelistBean.setPageIndex(index);
			sellerlivelistBean.setPageSize(size);
			sellerlivelistCall.setData(sellerlivelistBean);
			long count = liveRoomWapper.selectCountLiveroom(createlive.getUserId());
			//返回顺序需要按业务需求人工check
			while (hasnext) {
				sellerlivelistBean.setPageIndex(index);
				sellerlivelistCall.callService();
				Logger.verifyEquals("200", sellerlivelistCall.getString("Code"), "验证Code");
				Logger.verifyEquals(count, sellerlivelistCall.getJsonObject().get("Data").getAsJsonObject().get("Totals").getAsLong(), "验证count");
				JsonArray ja = sellerlivelistCall.getJsonObject().get("Data").getAsJsonObject().get("LiveList").getAsJsonArray();
				for (JsonElement jsonElement : ja) {
					boolean b=liveset.add(jsonElement.getAsJsonObject().get("Id"));
					if(!b){
						Logger.comment(jsonElement.getAsJsonObject().get("Id")+"重复");
					}
				}
				index++;
				if(liveset.size()>=count){
					hasnext=false;
				}
			}
			Thread.sleep(1);
			Logger.verifyEquals(count, Long.valueOf(liveset.size()), "验证不重复直播数size");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SellerLiveList_003() {
		Logger.start(true, "不输入userid失败");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
//			sellerlivelistBean.setUserId(createlive.getUserId());
			sellerlivelistBean.setPageIndex(1);
			sellerlivelistBean.setPageSize(10);
			sellerlivelistCall.setData(sellerlivelistBean);
			sellerlivelistCall.callService();
			Logger.verifyEquals("400", sellerlivelistCall.getString("Code"), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SellerLiveList_004() {
		Logger.start(true, "不输入pagesize返回空");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			sellerlivelistBean.setUserId(createlive.getUserId());
			sellerlivelistBean.setPageIndex(1);
//			sellerlivelistBean.setPageSize(10);
			sellerlivelistCall.setData(sellerlivelistBean);
			sellerlivelistCall.callService();
			Logger.verifyEquals("200", sellerlivelistCall.getString("Code"), "验证Code");
			Logger.verifyEquals(0,sellerlivelistCall.getJsonObject().get("Data").getAsJsonObject().get("LiveList").getAsJsonArray().size(), "验证无数据");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P2.class })
	@TestCase
	public void Tc_SellerLiveList_005() {
		Logger.start(true, "不输入pageindex失败");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			sellerlivelistBean.setUserId(createlive.getUserId());
//			sellerlivelistBean.setPageIndex(1);
			sellerlivelistBean.setPageSize(10);
			sellerlivelistCall.setData(sellerlivelistBean);
			sellerlivelistCall.callService();
			Logger.verifyEquals("500", sellerlivelistCall.getString("Code"), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SellerLiveList_006() {
		Logger.start(true, "查询买手直播列表成功删除列表不显示");
		try {
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			sellerlivelistBean.setUserId(createlive.getUserId());
			sellerlivelistBean.setPageIndex(1);
			sellerlivelistBean.setPageSize(10);
			sellerlivelistCall.setData(sellerlivelistBean);
			sellerlivelistCall.callService();
			Logger.verifyEquals("200", sellerlivelistCall.getString("Code"), "验证Code");
			long count = liveRoomWapper.selectCountLiveroom(createlive.getUserId());
			Logger.verifyEquals(count, sellerlivelistCall.getJsonObject().get("Data").getAsJsonObject().get("Totals").getAsLong(), "验证count");
			
			Logger.comment("新发一个直播然后关闭删除,查询总数没有增加证明删除不显示");
			createlive = LiveRoomCallService.CreateSellerLive(LiveRoomCallService.initCreatesellerliveBean(null));
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			LiveRoomCallService.CloseSellerLive(createlive.getId(), createlive.getUserId(), 8);
			LiveRoomCallService.RemoveSellerLive(createlive.getId(), createlive.getUserId());
			sellerlivelistCall.callService();
			Logger.verifyEquals("200", sellerlivelistCall.getString("Code"), "验证Code");
			Logger.verifyEquals(count, sellerlivelistCall.getJsonObject().get("Data").getAsJsonObject().get("Totals").getAsLong(), "验证count");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}